home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7067 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  111 lines

  1. Newsgroups: comp.lang.ada,comp.lang.c++
  2. Path: alexandria.organon.com!alexandria!jsa
  3. From: jsa@organon.com (Jon S Anthony)
  4. Subject: Re: on OO differnces between Ada95 and C++
  5. In-Reply-To: nabbasi@qualcomm.com's message of 20 Feb 1996 06:37:46 GMT
  6. Message-ID: <JSA.96Feb21134417@organon.com>
  7. Sender: news@organon.com (news)
  8. Organization: Organon Motives, Inc.
  9. References: <4gbq7q$g08@qualcomm.com>
  10. Date: Wed, 21 Feb 1996 18:44:17 GMT
  11.  
  12. In article <4gbq7q$g08@qualcomm.com> nabbasi@qualcomm.com (Nasser Abbasi) writes:
  13.  
  14. Use child packages.  For example,
  15.  
  16. >  Learning_Ada95_OO_features...
  17. > ...
  18. > Now the C++ way:
  19. > --------------- account.h -------------------
  20. > class Account
  21. > {
  22. >   public:
  23. >   typedef int Money_Type;
  24. >   Money_Type Balance;
  25. >   int        Account_Id;
  26. > };
  27. > -------------- saving_account.h -----------------
  28. > #include "saving_account.h" 
  29. > class Saving_Account : public Account
  30. > {
  31. >   public:
  32. >   Money_Type Interest;
  33. > };
  34. > --------------- main.cpp -----------------
  35. > #include <iostream.h>
  36. > #include "saving_account.h"  // Notice: no need to include to base class
  37. >                              // Account in account.h
  38. > main()
  39. > {
  40. > Saving_Account             The_Saving_Account;
  41. > Saving_Account::Money_Type  The_Balance;  // notice, Money_Type accessed
  42. >                                           // through Saving_Account and
  43. >                                           // no need to include the
  44. >                                           // base class account
  45. >  cout << "Balance?";
  46. >  cin  >> The_Balance;
  47. >  The_Saving_Account.Balance = The_Balance;
  48. > return 1;
  49. > }
  50.  
  51. Ada version (from your stuff...):
  52.  
  53. > ------------------ account.ads ---------------------------
  54. > package Account is
  55. >  type Money_Type is new integer range 1..100; -- just as an example
  56. >  type Account_Type is tagged
  57. >    record
  58. >      Balance    : Money_Type;
  59. >      Account_Id : Integer; 
  60. >    end record;
  61. > end Account;
  62.  
  63. ----------------- Account-Savings.ads ----------------
  64.  
  65. Package Account.Saving_Account is
  66.  
  67.  -- Note: all the stuff in Account is visible here
  68.  
  69.  type Saving_Account_Type is new Account.Account_Type with
  70.    record
  71.      Interest : Account.Money_Type;
  72.    end record;
  73.  
  74. end Saving_Account;
  75.  
  76. > --------------- main.adb ---------------------------
  77. with Account.Saving_Account;
  78. --
  79. -- Note: visibility of Account comes along with Saving_Account here.
  80.  
  81. with ada.text_io; use ada.text_io;
  82.  
  83. procedure main is
  84.  
  85.   package Money_IO is new Ada.Text_IO.Integer_IO(Account.Money_Type);
  86.   The_Saving_Account : Saving_Account.Saving_Account_Type;
  87.   The_Balance : Account.Money_Type; 
  88.   begin
  89.     Money_IO.Get(The_Balance);
  90.     The_Saving_Account.Balance := The_Balance;
  91. end main;
  92. -- 
  93. Jon Anthony
  94. Organon Motives, Inc.
  95. 1 Williston Road, Suite 4
  96. Belmont, MA 02178
  97.  
  98. 617.484.3383
  99. jsa@organon.com
  100.  
  101.